<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Recommend extends Model
{
  use Notifiable;
  protected $table = 'recommends';

  // 推荐位列表
  public static function getList($request)
  {
    $query = self::select('*');
    if (isset($request->keywords) && $request->keywords) {
      $query->where('title', 'like', '%' . $request->keywords . '%');
    }
    if (isset($request->sort) && $request->sort) {
      $query->orderBy($request->sort);
    } else {
      $query->orderBy('id');
    }
    $res = $query->paginate($request->size);
    return $res ? $res->toArray() : [];
  }

  // 推荐位详情
  public static function getInfo($request) {
    $query = self::select('*');
    if(isset($request->id) && $request->id) {
      $query->where('id', $request->id);
    }
    if(isset($request->code) && $request->code) {
      $query->where('code', $request->code);
    }
    $res = $query->first();
    if($res) {
      $res = $res->toArray();
      $res['content'] = json_decode($res['content']);
    } else {
      $res = [];
    }
    return $res;
  }
}
